package nuonuo.com.myapplication;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.util.List;

import nuonuo.com.aidl.Book;
import nuonuo.com.aidl.IBookManager;

public class MainActivity extends AppCompatActivity {

    private ServiceConnection mConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            IBookManager mManager = IBookManager.Stub.asInterface(iBinder);
            try {
                List<Book> bookList = mManager.getBookList();
                Log.i("mtag",bookList.size()+"");
                for (int i = 0; i < bookList.size(); i++) {
                    Log.i("mtag",bookList.get(i).getmBookName());
                }
                mManager.addBook(new Book("99","html5"));
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent();
        intent.setAction("nuonuo.com.aidl");
        intent.setPackage("nuonuo.com.aidl");
        bindService(intent, mConn, Context.BIND_AUTO_CREATE);
    }
}
